package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.SiteDao;
import cn.hls.entity.Site;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;

/**
  *@fileName: SiteDaoImpl.java
  *@package: cn.hls.dao.impl
　*@Description: 站点数据操作接口实现类
　*@author: yinghui.zhang
　*@email: uuhui@163.com
  *@date: 2016年6月26日
 *
 */
@Component("siteDao")
public class SiteDaoImpl extends BaseDaoImpl<Site> implements SiteDao {

	@Override
	public PageInfo<Site> loadSiteInfo(Site entity, PageInfo<Site> page) throws Exception {
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from Site p left join  fetch p.company com ");
		// 定义汇总Hql语句
		StringBuilder counthql = new StringBuilder("select count(p.id) from Site p left join  p.company com ");
		//定义查询条件hql
		StringBuilder conditionHql=new StringBuilder("where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按名称查询
			 if(StringUtils.hasText(entity.getName())){
				 conditionHql.append("and name like ? ");
				 paramList.add("%"+entity.getName().trim()+"%");
			 }
			 //按身份证查询
			 if(StringUtils.hasText(entity.getContractIdno())){
				 conditionHql.append("and contractIdno like ? ");
				 paramList.add("%"+entity.getContractIdno().trim()+"%");
			 }
			 
			 if(entity.getType()!=null){
				 conditionHql.append("and p.type=? ");
				 paramList.add(entity.getType());
			 }
			 if(entity.getStatus()!=null){
				 conditionHql.append("and p.status=? ");
				 paramList.add(entity.getStatus());
			 }
			 
			 //按企业
			 if(entity.getCompany()!=null){
				 //按企业编号查询
				 if(entity.getCompany().getId()!=null){
					 conditionHql.append("and com.id=? ");
					 paramList.add(entity.getCompany().getId());
				 }
				 //按名称查询
				 if(StringUtils.hasText(entity.getCompany().getName())){
					 conditionHql.append("and com.name like ? ");
					 paramList.add("%"+entity.getCompany().getName().trim());
				 }
				 //按承运企业编号查询
				 if(entity.getCompany().getCarryCompanyId()!=null){
					 conditionHql.append("and com.carryCompanyId=? ");
					 paramList.add(entity.getCompany().getCarryCompanyId());
				 }
			 }
			 
		}
		counthql. append(conditionHql);
		selectHql.append(conditionHql);
		selectHql.append(" order by p.id desc");
		return find(selectHql.toString(), counthql.toString(), paramList.toArray(), page);
	}
	

	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("update Site set deleted=? where id in (");
		List<Object>params=new ArrayList<Object>();
		params.add(true);
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.UPDATE_ERROR,"更新人员信息失败" );
		}
	}

	 
	 
	@Override
		public boolean exists(Site entity)throws Exception{
			StringBuilder selectHql=new StringBuilder("select count(id) from Site p where deleted=? ");
			List<Object>params=new ArrayList<Object>();
			params.add(false);
			if(entity!=null){
				if(StringUtils.hasText(entity.getName())){//姓名
					selectHql.append("and name=? ");
					params.add(entity.getName().trim());
				}
				if(entity.getCompany()!=null){
					if(entity.getCompany().getId()!=null){
						selectHql.append("and p.company.id=? ");
						params.add(entity.getCompany().getId());
					}
				}
			}
			Query query=this.getSession().createQuery(selectHql.toString());
			for(int i=0;i<params.size();i++){
				query.setParameter(i, params.get(i));
			}
			Long c = Long.valueOf(((Number) query.uniqueResult()).longValue());
			return c.intValue()>0?true:false;
	}

	@Override
	public List<Site> loadSiteList(Site entity) throws Exception {
		StringBuilder hql=new StringBuilder("from Site p inner join  fetch p.company com  where p.deleted=? ");
		List<Object>paramList=new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按名称查询
			 if(StringUtils.hasText(entity.getName())){
				 hql.append("and name like ? ");
				 paramList.add("%"+entity.getName().trim()+"%");
			 }
			 
			 if(entity.getCompany()!=null){
				 //按编号查询
				 if(entity.getCompany().getId()!=null){
					 hql.append("and com.id=? ");
					 paramList.add(entity.getCompany().getId());
				 }
				 //按承运企业编号查询
				 if(entity.getCompany().getCarryCompanyId()!=null){
					 hql.append("and com.carryCompanyId=? ");
					 paramList.add(entity.getCompany().getCarryCompanyId());
				 }
			 }
			 
			 if(entity.getType()!=null){
				 hql.append("and p.type=? ");
				 paramList.add(entity.getType());
			 }
			 if(entity.getStatus()!=null){
				 hql.append("and p.status=? ");
				 paramList.add(entity.getStatus());
			 }
			 
			 //按企业
			 if(entity.getCompany()!=null){
				 if(entity.getCompany().getId()!=null){
					 hql.append("and com.id=? ");
					 paramList.add(entity.getCompany().getId());
				 }
				 if(StringUtils.hasText(entity.getCompany().getName())){
					 hql.append("and com.name like ? ");
					 paramList.add("%"+entity.getCompany().getName().trim());
				 }
			 }
			 
		}
		return findByHql(hql.toString(), paramList.toArray());
	}
	
}
